public  class Cbl_ServicePlanToCase {
    

    public static void ServicePlanToCas(List<ServicePlan__c> list_ser)
    {
        Set<String> set_str=new Set<String>();
        Set<String> set_month=new Set<String>();

        Id srd=Utils.getRecordTypeIdByDevNameAndObjName('ServicePlanCase', 'Case');
        Id mrd =Utils.getRecordTypeIdByDevNameAndObjName('MaintenanceOrder', 'Case');
        // [select id,DeveloperName from RecordType where DeveloperName='ServicePlanCase' and SobjectType = 'Case'];

        for(ServicePlan__c sp:list_ser)
        {
           
            if(sp.Employeenumbergc__c!=null )
            {
                set_str.add(sp.Employeenumbergc__c);
            }

             if(sp.EmployeeNumber__c!=null )
            {
                set_str.add(sp.EmployeeNumber__c);
            }

            if(sp.January__c==true)
            {
                set_month.add('1-'+sp.Id);
            }
            if(sp.February__c==true)
            {
                set_month.add('2-'+sp.Id);
            }
            if(sp.March__c==true)
            {
                set_month.add('3-'+sp.Id);
            }
            if(sp.April__c==true)
            {
                set_month.add('4-'+sp.Id);
            }
            if(sp.May__c==true)
            {
                set_month.add('5-'+sp.Id);
            }
            if(sp.June__c==true)
            {
                set_month.add('6-'+sp.Id);
            }
            if(sp.July__c==true)
            {
                set_month.add('7-'+sp.Id);
            }
            if(sp.August__c==true)
            {
                set_month.add('8-'+sp.Id);
            }
            if(sp.September__c==true)
            {
                set_month.add('9-'+sp.Id);
            }
            if(sp.October__c==true)
            {
                set_month.add('10-'+sp.Id);
            }
            if(sp.November__c==true)
            {
                set_month.add('11-'+sp.Id);
            }
            if(sp.December__c==true)
            {
                set_month.add('12-'+sp.Id);
            }
        }
        
        //update 重点客户
        UpdateAcc();
        
        //生成个案
        Map<String,User> map_us=new Map<String,User>();
        map_us=UserOwner(set_str);
        List<case> list_case=new List<case>();
        for(ServicePlan__c scase:list_ser)
        {
                
                if(scase.Servicetype__c == null ){
                    scase.addError('服务类型不能为空');
                    continue;
                }
                Integer i=1;
                for(String str:set_month)
                {
                    String month=str.split('-')[0];
                    String scId=str.split('-')[1];
                    if(scId==scase.Id)
                    {   
                        Integer j = (scase.NumberofTimesPerM__c == null || scase.NumberofTimesPerM__c == 0 ) ? 1: Integer.valueOf(scase.NumberofTimesPerM__c);

                        for (Integer k = 0; k < j ; k ++ ) {
                            case c1=new case();
                            c1.AccountId=scase.Account__c;
                            c1.Product_Info__c=scase.Product_Info__c;
                            // c1.ProductLine__c=scase.ProductLine__c;
                            c1.Year__c=scase.Year__c;
                            c1.Month__c=Integer.valueOf(month);
                            c1.ServicePlan__c=scase.Id;
                            c1.Origin='服务计划';
                            c1.RecordTypeId=srd;
                            c1.TypeofServicePlan__c = scase.TypeofServicePlan__c;
                            c1.Servicetype__c = scase.Servicetype__c;
                           // c1.OwnerId = map_us.get(scase.EmployeeNumber__c).Id;
                            //生成个案时把负责人指定为工程师
                            // 如果负责人是 工程部的 指定为 组长
                            User u  = map_us.get(scase.EmployeeNumber__c);
                            User gc = map_us.get(scase.Employeenumbergc__c);
                            if(scase.Servicetype__c == '工程'){
                                c1.User__c = u.Id;
                                c1.Engineer__c = gc.Id;
                                if(gc.Profile.Name.contains('经销商')){
                                    c1.RecordTypeId=srd;
                                }
                            }else if(scase.Servicetype__c == '应用' ){
                                c1.User__c = u.Id;
                            }

                        
                            c1.Subject='重点客户巡回计划-'+scase.AccountName__c+'-'+month+'月';
                            c1.Description=scase.AccountName__c+'-'+'-第'+i+'次-'+scase.Owner__c;
                            i++;
                            list_case.add(c1);   
                        }
                        
                        
                    }
                }
                
            // }else
            // {
            //     scase.addError('技术负责人未匹配成功');
            // }
            
        }
        insert list_case;
    }
  
    ///新建的时候 修改所有人
	public static void UpdateSerplanOwner(List<ServicePlan__c> list_new){
    	Set<String> userName = new Set<String>();
        Set<Id> spId = new Set<Id>();
        for (ServicePlan__c sp : list_new) {
            if (String.isBlank(sp.EmployeeNumber__c) == false  ) {
                userName.add(sp.EmployeeNumber__c);
 
                spId.add(sp.Id);
            }

        }
        
        if (spId.size() > 0) {
            // 检索用户
            Map<String, User> map_us = UserOwner(userName);
            System.debug('==>'+map_us);
            for (ServicePlan__c sp : list_new) {

                if (map_us.containsKey(sp.EmployeeNumber__c) == true  ) {
                	///修改服务计划的所有人
                    User eu =  map_us.get(sp.EmployeeNumber__c);
                 	if(eu.IsActive)sp.OwnerId = eu.Id;
                    sp.Department__c = map_us.get(sp.EmployeeNumber__c).UserDepartment__c;
                }
        	}
        }
        
        
    }

    ///修改员工编号的时候修改 服务计划所有人技术负责人 服务请求所有人   工程师
    public static void UpdateCaseOwner(List<ServicePlan__c> list_ser, Map<Id, ServicePlan__c> oldMap) {
        Set<String> userName = new Set<String>();
        Set<Id> spId = new Set<Id>();
        for (ServicePlan__c sp : list_ser) {
            ServicePlan__c oldSp = oldMap.get(sp.Id);
            if (
                    (oldSp.EmployeeNumber__c != sp.EmployeeNumber__c && String.isBlank(sp.EmployeeNumber__c) == false )

                    || (oldSp.Employeenumbergc__c != sp.Employeenumbergc__c && String.isBlank(sp.Employeenumbergc__c) == false )
                ) {

                if(sp.EmployeeNumber__c != null )userName.add(sp.EmployeeNumber__c);
                if(sp.Employeenumbergc__c != null )userName.add(sp.Employeenumbergc__c);

                spId.add(sp.Id);
            }
        }

        if (spId.size() > 0) {
            // 检索用户
            Map<String, User> map_us = UserOwner(userName);
            // 检索个案
            Map<Id, List<Case>> caseMap = new Map<Id, List<Case>>();
            for (Case c : [select Id,Engineer__c, ServicePlan__c,User__c from Case where ServicePlan__c in :spId and Status != '关闭']) {
                List<Case> clist = new List<Case>();
                if (caseMap.containsKey(c.ServicePlan__c) == true) clist = caseMap.get(c.ServicePlan__c);
                clist.add(c);
                caseMap.put(c.ServicePlan__c, clist);
            }

            List<Case> updList = new List<Case>();
            for (ServicePlan__c sp : list_ser) {
                if (map_us.size() == 0) {
                    sp.addError('员工编号未匹配成功');
                    return;
                }
                //if (map_us.containsKey(sp.EmployeeNumber__c) == true) {
                    // 修改服务计划的责任人
                    System.debug(map_us.get(sp.EmployeeNumber__c));
                    sp.Owner__c = map_us.get(sp.EmployeeNumber__c).Name;
                    // 修改对应的个案
                   if(caseMap.containsKey(sp.Id)){
                       for (Case c : caseMap.get(sp.Id)) {
                           User u  = map_us.get(sp.EmployeeNumber__c);
                           User eg = map_us.get(sp.Employeenumbergc__c);
                         //  c.OwnerId = u.Id;

                        if(sp.Servicetype__c == '应用'){
                            c.User__c = u.Id;
                        }else if(sp.Servicetype__c == '工程'){
                            c.User__c = u.Id;
                            c.Engineer__c = eg.Id;
                        }
                        // if(u.UserDepartment__c == '客户服务中心(工程部)'){
                        // 	c.User__c = u.Id;
                        //     c.Engineer__c = eg.Id;
                        // }else{
                        // 	c.Engineer__c = u.Id;
                        // }
                           // c.Engineer__c = map_us.get(sp.EmployeeNumber__c).Id;
                           
                           
                            //if(map_us.get(sp.EmployeeNumber__c).Id !=null)c.OwnerId = map_us.get(sp.EmployeeNumber__c).Id;
                            //if(map_us.get(sp.EmployeeNumber__c).Id !=null)c.Engineer__c = map_us.get(sp.EmployeeNumber__c).Id;
                            updList.add(c);
                        }
                   }
                    
                    ///修改服务计划的所有人
                	sp.OwnerId = map_us.get(sp.EmployeeNumber__c).Id;

                // } else {
                //     sp.addError('员工编号未匹配成功');
                //     continue;
                // }
            }
            if (updList.size() > 0) update updList;
        }
    }
    
    public static Map<String,User> UserOwner(Set<String> set_str)
    {
        List<User> list_user=[select Id, Alias, Profile.Name ,Name,UserDepartment__c,IsActive, EmployeeNumber from User where EmployeeNumber in:set_str ];
        Map<String,User> map_us=new Map<String,User>();
        for(User u:list_user){
            map_us.put(u.EmployeeNumber,u);
        }
        return map_us;
    }
    
    ///是否为重点 客户
    public static void UpdateAcc()
    {
      
        Set<Id> oldSet = new Set<Id>();

        Map<Id,Boolean> vipAccMap = new Map<Id,Boolean>();
        Map<Id,Boolean> ruleAccMap = new Map<Id,Boolean>();

        ///找到当年所有的服务计划 对应的客户 --->重点客户
        for(ServicePlan__c sp: [select Account__c,Account__r.Iskeynote__c ,Account__r.IFuserule__c from ServicePlan__c where Year__c=:System.now().year()]){
            if(sp.Account__c  != null)vipAccMap.put(sp.Account__c,sp.Account__r.Iskeynote__c);ruleAccMap.put(sp.Account__c,sp.Account__r.IFuserule__c);
        }
        List<Account> accList = new List<Account>();
        ///找到 原来 所有的重点客户
        List<Account> list_Acc=[select Id, Iskeynote__c,IFuserule__c from Account where Iskeynote__c = true]; 
       for(Account acc:list_Acc)
        {
           if(vipAccMap.containsKey(acc.Id)  ){

           }else{///将今年不为重点客户的改为 false
                acc.Iskeynote__c = false;
                acc.IFuserule__c = !acc.IFuserule__c;
                accList.add(acc);
           }
        }
        for (Id accid  :vipAccMap.keySet()  ) {
            if(vipAccMap.get(accid) == false){///将之前不为重点客户的改为 true;
                accList.add(new Account(id=accid,Iskeynote__c = true, IFuserule__c = !ruleAccMap.get(accid)));
            }
        }


        if(accList.size() > 0)update accList;
       // update list_Acc;
    }
    

}